﻿using MicroCloud;
using MicroCloud.Extensions;
using MicroCloud.Options;
using MicroCloud.Packs;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;

namespace Ruirun.Mes
{
    [Description($"{nameof(MesDbContext)}迁移模块")]
    public class DbContextMigrationPack : PackBase
    {
        //字段
        private MicroCloudOptions _options;

        /// <summary>
        /// 获取 模块级别，级别越小越先启动
        /// </summary>
        public override PackLevel Level => PackLevel.Application;

        /// <summary>
        /// 将模块服务添加到依赖注入服务容器中
        /// </summary>
        /// <param name="services">依赖注入服务容器</param>
        /// <returns></returns>
        public override IServiceCollection AddServices(IServiceCollection services)
        {
            _options = services.GetMicroCloudOptions();
            var dbContextOptions = _options?.GetDbContextOptions(typeof(MesDbContext));
            if (dbContextOptions?.DatabaseType == null)
            {
                return services;
            }
            var builder = services.GetMicroCloudBuilder();
            switch (dbContextOptions.DatabaseType)
            {
                case DatabaseType.SqlServer:
                    builder.AddPack<SqlServerDbContextMigrationPack>();
                    break;
                case DatabaseType.MySql:
                    builder.AddPack<MySqlDbContextMigrationPack>();
                    break;
                case DatabaseType.Oracle:
                    builder.AddPack<OracleDbContextMigrationPack>();
                    break;
                case DatabaseType.PostgreSql:
                    builder.AddPack<PostgreSqlDbContextMigrationPack>();
                    break;
                case DatabaseType.Sqlite:
                    builder.AddPack<SqliteDbContextMigrationPack>();
                    break;
            }

            return services;
        }

        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UsePack(IServiceProvider provider)
        {
            IsEnabled = true;
        }

    }
}